import Vue from 'vue'
import App from './App'

import router from './router'
import axios from 'axios'
import { Message,Loading,Dialog,Button,Select,Option,Image ,Autocomplete,Input,Table,TableColumn,
    Dropdown,
    DropdownMenu,
    Tooltip,
    DropdownItem,} from 'element-ui'

// axios.defaults.baseURL = 'https://api.huoxingy.com/v1/web/';


// 根据环境来选择api地址
let baseUrl = window.location.hostname;
console.log(baseUrl)
switch (baseUrl) {
    case "localhost":
        axios.defaults.baseURL = 'https://staging-api.huoxingy.com/v1/web/';
        window.baseURL = 'https://staging-api.huoxingy.com/';
        // axios.defaults.baseURL = 'https://prod-api.huoxingy.com/v1/web/';
        break;
    case "staging-pc-order.huoxingy.com":
        axios.defaults.baseURL = 'https://staging-api.huoxingy.com/v1/web/';
        window.baseURL = 'https://staging-api.huoxingy.com/';
        break;
    case "prod-test-pc-order.huoxingy.com":
        axios.defaults.baseURL = 'https://prod-test-api.huoxingy.com/v1/web/';
        window.baseURL = 'https://staging-api.huoxingy.com/';
        break;
    default:
        axios.defaults.baseURL = 'https://prod-api.huoxingy.com/v1/web/';
        window.baseURL = 'https://prod-api.huoxingy.com/';
        break;
}
//






axios.defaults.headers.post['Content-Type'] = 'application/json';

/*
    设置请求拦截器
*/
/*axios.interceptors.request.use(function(config) {
    let token = sessionStorage.getItem('token');
    if (token) {
        config.headers.Authorization = token;
    }
    return config;
}, function(error) {
    return Promise.reject(error);
});*/

/*
    添加响应拦截器
*/
axios.interceptors.response.use(function(response) {
    return response;
}, function(error) {
    if (error.response) {
        let tipText;
        error.response.data.message?tipText=error.response.data.message:tipText=error.response.data;
        if ( error.response.status == 401 ) {
            window.location.href = window.location.href.split('#')[0]+'#/login';
            tipText = '帐号失效，请重新登录';
        } else if ( error.response.status == 403 ) {
            // window.location.href = window.location.href.split('#')[0]+'#/login';
            tipText = '权限不足';
        }
        Message.error({
            message: tipText,
            duration:5000
        })
    }
    return Promise.reject(error);
});


/*
    添加自定义过滤器
*/
Vue.filter('numberToDate', function ( val ) {
    let dateStr = new Date(val).toLocaleDateString();
    return dateStr;
})

Vue.filter('numberToTime', function ( val ) {
    let date = new Date(val);
    let dateStr = (date.getHours()<10?'0'+date.getHours():date.getHours()) + ':' + (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes());
    return dateStr;
})

Vue.filter('numberToAllDate', function ( val ) {
    if ( !val ) {
        return '';
    }
    let date = new Date(val);
    let dateStr =date.getFullYear()+'/'+(date.getMonth()+1)+'/'+date.getDate()+'-'+ (date.getHours()<10?'0'+date.getHours():date.getHours()) + ':' + (date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes());
    return dateStr;
})

Vue.prototype.$uuid = function ( len, radix ) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    var uuid = [], i;
    radix = radix || chars.length;
    if (len) {
        // Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
    } else {
        // rfc4122, version 4 form
        var r;
 
        // rfc4122 requires these characters
        // uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
 
        // Fill in random data.  At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for ( i = 0; i < 32; i++ ) {
            if ( !uuid[i] ) {
                r = 0 | Math.random()*62;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
}

Vue.prototype.$http = axios

Vue.use(Loading.directive)
Vue.use(Select)
Vue.use(Option)
Vue.use(Dialog)
Vue.use(Button)
Vue.use(Image)
Vue.use(Autocomplete)
Vue.use(Input)
Vue.use(Dropdown)
Vue.use(DropdownMenu)
Vue.use(DropdownItem)
Vue.use(Tooltip)
Vue.use(Table)
Vue.use(TableColumn)
Vue.prototype.$loading = Loading.service
Vue.prototype.$message = Message

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  	el: '#app',
  	router,
  	template: '<App/>',
  	components: { App }
})
